package cn.kgc.service.impl;

import cn.kgc.dto.QueryCondition;
import cn.kgc.entity.AppInfo;
import cn.kgc.mapper.AppInfoMapper;
import cn.kgc.service.AppCategoryService;
import cn.kgc.service.AppInfoService;
import cn.kgc.service.DataDictionaryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class AppInfoServiceImpl implements AppInfoService {

    @Autowired
    AppInfoMapper appInfoMapper;
    @Autowired
    DataDictionaryService dataDictionaryService;
    @Autowired
    AppCategoryService appCategoryService;
    @Override
    public PageInfo<AppInfo> findAppInfo(QueryCondition queryCondition, Integer devId, Integer pageNum, Integer pageSize) {
        //开启分页
        PageHelper.startPage(pageNum, pageSize);
        //组织查询条件
        Example query = new Example(AppInfo.class);
        Example.Criteria whereCluse = query.and().andEqualTo("devId", devId);//开发者查询条件
        if (queryCondition != null) {
            whereCluse.andLike("softwareName", !StringUtils.isBlank(queryCondition.getQuerySoftwareName()) ? "%" + queryCondition.getQuerySoftwareName() + "%" : null)
                    .andEqualTo("status", queryCondition.getQueryStatus())
                    .andEqualTo("flatformId", queryCondition.getQueryFlatformId())
                    .andEqualTo("categoryLevel1", queryCondition.getQueryCategoryLevel1())
                    .andEqualTo("categoryLevel2", queryCondition.getQueryCategoryLevel2())
                    .andEqualTo("categoryLevel3", queryCondition.getQueryCategoryLevel3());
        }
        //排序
        query.orderBy("creationDate").desc();
        List<AppInfo> appInfos = appInfoMapper.selectByExample(query);
        for (AppInfo appInfo : appInfos) {
            appInfo.setFlatformName(dataDictionaryService.findDataDictionary("APP_FLATFORM",appInfo.getFlatformId()).getValueName());
            appInfo.setStatusName(dataDictionaryService.findDataDictionary("APP_STATUS",appInfo.getStatus()).getValueName());
            appInfo.setCategoryLevel1Name(appCategoryService.findCategoryById(appInfo.getCategoryLevel1()).getCategoryName());
            appInfo.setCategoryLevel2Name(appCategoryService.findCategoryById(appInfo.getCategoryLevel2()).getCategoryName());
            appInfo.setCategoryLevel3Name(appCategoryService.findCategoryById(appInfo.getCategoryLevel3()).getCategoryName());
        }
        return new PageInfo<>(appInfos);
    }

    @Override
    public int addApp(AppInfo appInfo) {
        return appInfoMapper.insert(appInfo);
    }


}
